import router, { constantRoutes } from '@/router'
import { getRouters } from '@/api/menu'
import Layout from '@/layout/index'
const permission = {
  state: {
    routes: [],
    addRoutes: [],
    defaultRoutes: [],
    topbarRouters: [],
    sidebarRouters: [],
    permissionList:[],
  },
  mutations: {
    SET_ROUTES: (state, routes) => {
      state.addRoutes = routes
      state.routes = constantRoutes.concat(routes)
    },
    SET_DEFAULT_ROUTES: (state, routes) => {
      state.defaultRoutes = constantRoutes.concat(routes)
    },
    SET_TOPBAR_ROUTES: (state, routes) => {
      state.topbarRouters = routes
    },
    SET_SIDEBAR_ROUTERS: (state, routes) => {
      state.sidebarRouters = routes
    },
  },
  actions: {
    // 生成路由
    GenerateRoutes({ commit }) {
      return new Promise((resolve, reject) => {
        // 向后端请求路由数据
        getRouters().then(res => {
          
          if (res.data) {
            //获取按钮权限
            const listPKey = res.data.userPermList.filter(obj=>{
              return !(Object.prototype.hasOwnProperty.call(obj, 'permKey') && (obj.permKey === ''));
            })
            listPKey.map(item=>{
              permission.state.permissionList.push(item.permKey)
            })
            //生成路由信息
            const filteredArr = res.data.userPermList.filter((obj) => {
              return !(Object.prototype.hasOwnProperty.call(obj, 'permKey') && (obj.permKey !== ''));
            })
            const  asyncRoutes= listRouter(filteredArr)
            console.log(asyncRoutes);
            router.addRoutes(asyncRoutes);
            commit('SET_ROUTES', asyncRoutes)
            commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(asyncRoutes))
            commit('SET_DEFAULT_ROUTES', asyncRoutes)
            commit('SET_TOPBAR_ROUTES', asyncRoutes)
            resolve(asyncRoutes)
          } else {
            reject(res)
          }
        })
      })
    }
  }
}
function listRouter(RouterData) {
  let newRouter = []
  let newRouterEnd = []
  RouterData.map(item => {
    item.children = []
    if (item.upId == '0') {
      newRouter.push(item)
    }
  })
  newRouter.map(item => {
    RouterData.map(Titem => {
      if (item.permId == Titem.upId) {
        let indexAV = Titem.permUrl.indexOf('/')
    let indexBV = Titem.permUrl.indexOf('/',indexAV+1)
        let data = {
          name : Titem.permName,
          path: Titem.permUrl,
          component: loadView(Titem.permUrl),
          redirect: Titem.redirectUrl,
          meta: {
            title: Titem.permName,
            icon: Titem.permIcon
          }
        }
        item.children.push(data)
      }
    })
    let indexA = item.redirectUrl.indexOf('/')
    let indexB = item.redirectUrl.indexOf('/',indexA+1)
let Fdata = {
      path: item.redirectUrl.slice(0,indexB),
      component: Layout,
      children:item.children,
      redirect: item.redirectUrl,
      meta: {
        title: item.permName,
        icon: item.permIcon
      }
    }
    newRouterEnd.push(Fdata)
  })
  return newRouterEnd
}
//生成动态路由component路径
export const loadView = (view) => {
  if (process.env.NODE_ENV === 'development') {
    return (resolve) => require([`@/views${view}`], resolve)
  } else {
    // 使用 import 实现生产环境的路由懒加载
    return () => import(`@/views${view}`)
  }
}

export default permission
